package com.sh.personnel.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

/**
 * @author 林思浩
 * @date 2020/03/12 12:42
 */
@Component
public class EmailUtil {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    public boolean sendSimpleMail(String toMail, String title, String text) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(javaMailSender.getUsername());
        mailMessage.setTo(toMail);
        mailMessage.setSubject(title);
        mailMessage.setText(text);
        try {
            javaMailSender.send(mailMessage);
        } catch (MailException e) {
            return false;
        }
        return true;
    }


    public static boolean validMail(String email) {
        return email.matches("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
    }

}
